package com.lagou.cmd;

import com.lagou.pojo.Article;
import com.lagou.service.IArticleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.Calendar;
import java.util.List;

@Controller
public class ArticleCmd {
    @Autowired
    private IArticleService articleService;

    @RequestMapping("/index")
    public String toIndexPage(Model model) {
        List<Article> articleList = articleService.findAll();
        model.addAttribute("articleList", articleList);
        model.addAttribute("currentYear", Calendar.getInstance().get(Calendar.YEAR));
        return "client/index";
    }

    @RequestMapping("/list/{currPage}/{pageSize}")
    public String getList(Model model, @PathVariable("currPage") int currPage, @PathVariable("pageSize") int pageSize) {
        // ModelAndView modelAndView = new ModelAndView();
        List<Article> articleList = articleService.findByPage(currPage, pageSize);
        int allCount = articleService.findAll().size();
        int nextPage = currPage + pageSize;
        int prePage = currPage - pageSize <= 0 ? 0 : currPage - pageSize;
        int lastPage = (allCount + 1) / pageSize + 1;
        model.addAttribute("articleList", articleList);
        model.addAttribute("allCount", allCount);
        model.addAttribute("currPage", currPage);
        model.addAttribute("nextPage", nextPage);
        model.addAttribute("prePage", prePage);
        model.addAttribute("lastPage", lastPage);
        return "client/index";
    }

}
